package stack_queue

func NewMyQueue2(cap int) *MyQueue2 {
	return &MyQueue2{cap: cap, size: 0, pushI: 0, popI: 0, arr: make([]interface{}, cap)}
}

type MyQueue2 struct {
	size  int
	cap   int
	pushI int
	popI  int
	arr   []interface{}
}

func (s *MyQueue2) Push(val int) {
	if s.size == s.cap {
		panic("Queue was fulled can't push !!!")
	}
	s.size++
	s.arr[s.pushI] = val
	s.pushI = s.getNextIndex(s.pushI)
}

func (s *MyQueue2) Pop() int {
	if s.size == 0 {
		panic("Queue was empty can't pop !!!")
	}
	s.size--
	ans := s.arr[s.popI].(int)
	s.popI = s.getNextIndex(s.popI)
	return ans
}

func (s *MyQueue2) IsEmpty() bool {
	return s.size == 0
}

func (s *MyQueue2) getNextIndex(index int) int {
	ans := index + 1
	if ans == s.cap {
		return 0
	}
	return ans
}
